﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Remoter
{
    public abstract class CommandBase : ICommand
    {
        public CommandBase() { }

        public CommandBase(SerialPort port, string cmdText)
        {
            this.CommandText = cmdText;
            this.Port = port;
        }

        public string CommandText { get; set; }
        public SerialPort Port { get; set; }

        public virtual string Execute()
        {
            try
            {
                byte[] data = this.CommandText.ToByteArray();
                this.Port.Write(data, 0, data.Length);
                return "success";
            }
            catch (FormatException)
            {
                return "failed";
            }
        }
    }
}
